package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductSpecService;
import com.atguigu.spzx.model.entity.product.ProductSpec;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "商品规格管理模块")
@RequestMapping("/admin/product/spec")
public class ProductSpecController {
    @Resource
    ProductSpecService productSpecService;
    @Operation(summary = "查询所有商品规格")
    @GetMapping
    public Result<List<ProductSpec>> listAll(){
        List<ProductSpec> productSpecs = productSpecService.listAll();
        return Result.ok(productSpecs);
    }
    @Operation(summary = "更新商品规格")
    //前端需要将规格数据封装为 json数组字符串
    @PutMapping
    public Result update(@RequestBody ProductSpec productSpec){
        productSpecService.updateById(productSpec);
        return Result.ok(null);
    }
    @Operation(summary = "新增商品规格")
    //前端需要将规格数据封装为 json数组字符串
    @PostMapping
    public Result save(@RequestBody ProductSpec productSpec){
        productSpecService.save(productSpec);
        return Result.ok(null);
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Long id){
        productSpecService.deleteById(id);
        return Result.ok(null);
    }
    @Operation(summary = "分页查询商品规格")
    @GetMapping("page/{pageNum}/{pageSize}")
    public Result<PageInfo<ProductSpec>> page(@PathVariable("pageNum")Integer pageNum,
                                              @PathVariable("pageSize")Integer pageSize){
        PageInfo<ProductSpec> pageInfo = productSpecService.page(pageNum,pageSize);
        return Result.ok(pageInfo);
    }
}
